package com.cqut.hfh.ordering.controller;

import com.cqut.hfh.ordering.pojo.FoodMerchant;
import com.cqut.hfh.ordering.pojo.FoodMerchantVo;
import com.cqut.hfh.ordering.service.IMerchantService;
import com.cqut.hfh.ordering.utils.EntityIdutils;
import com.cqut.hfh.ordering.utils.Message;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * create by gtt on 2019/6/25
 */
@Controller
@RequestMapping("/merchant")
public class MerchantController {

    @Resource
    private IMerchantService merchantService;

    /**
     * 获取所有的商家信息
     * @param limit
     * @param page
     * @return
     */
    @RequestMapping(path = "/findAlls",produces = {"text/html;charset=utf-8"})
    @ResponseBody
    public String findAlls(@RequestParam("limit") String limit,
                           @RequestParam("page") String page){
        List<Map<String, Object>> merchant_list = merchantService.getMerchant(limit, page);
        for( Map<String, Object> map : merchant_list){
            if( map.get("isva").toString().equals("1")){
                map.put("isva", "有效");
            }
            if( map.get("isva").toString().equals("0")){
                map.put("isva", "无效");
            }
            DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String tsStr = sdf.format(map.get("optime"));
            map.put("optime", tsStr);
        }
        Integer count = merchantService.getCount();
        return Message.getReturnMsg(0,"", count, merchant_list);
    }

    /**
     * 根据商店名称查询商店信息
     * @param limit
     * @param page
     * @param merchant_name
     * @return
     */
    @RequestMapping(path = "/findMerchantsByName",produces = {"text/html;charset=utf-8"})
    @ResponseBody
    public String findMerchantsByName(@RequestParam("limit") String limit,
                                      @RequestParam("page") String page,
                                      @RequestParam("merchant_name") String merchant_name){
        List<Map<String, Object>> merchants_list = merchantService.getMerchantsByName(limit, page, merchant_name);

        for( Map<String, Object> map : merchants_list){
            if( map.get("isva").toString().equals("1")){
                map.put("isva", "有效");
            }
            if( map.get("isva").toString().equals("0")){
                map.put("isva", "无效");
            }
            DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String tsStr = sdf.format(map.get("optime"));
            map.put("optime", tsStr);
        }

        Integer count = merchantService.getCount();

        return Message.getReturnMsg(0,"", count, merchants_list);
    }

    /**
     * 根据商家id删除商家以及商家-类型
     * @param merchantId
     * @return
     */
    @RequestMapping(path = "/deletes",produces = {"text/html;charset=utf-8"})
    @ResponseBody
    public String deletes(@RequestParam("merchantId") String merchantId){
        int temp = merchantService.delByMerchantId(merchantId);
        if( temp == 1){
            return "{\"msg\":\"ok\"}";
        }
        return "{\"msg\":\"fail\"}";
    }

    /**
     * 通过商家id找到商家信息
     * @param merchantId
     * @return
     */
    @RequestMapping("/findById")
    @ResponseBody
    public FoodMerchantVo findById(String merchantId){
        Map<String, Object> result_map = merchantService.findById(merchantId);
        //FoodMerchantVo e = new FoodMerchantVo();
        FoodMerchantVo e=new FoodMerchantVo();
        e.setProperties(result_map);
        return e;
    }

    /**
     * 修改商家信息
     * @param merchant
     * @return
     */
    @RequestMapping(path = "/update")
    @ResponseBody
    public String update(FoodMerchantVo merchant){
        merchant.setOptime(new Date());
        if( merchant.getMerchant_id() != null) {
            String address=dealAddress(merchant);

            FoodMerchant f = new FoodMerchant();
            f.setProperties(merchant.toMap());

            if (address.equals("")) {
                int temp = merchantService.updateMerchant(f, merchant.getPic_site());
            }else {
                merchant.setMerchant_site(dealAddress(merchant));
                merchantService.updateMerchant(f, merchant.getPic_site());
            }
        }
        return "";
    }

    /**
     * 添加商家
     * @param foodMerchantVo
     * @return
     */
    @RequestMapping(path = "/save",produces = {"text/html;charset=utf-8"})
    @ResponseBody
    public String save(FoodMerchantVo foodMerchantVo){
        foodMerchantVo.setOptime(new Date());
        String id=foodMerchantVo.getMerchant_id();
        if( foodMerchantVo.getMerchant_id() == null) {

            foodMerchantVo.setMerchant_id(EntityIdutils.getId());

            FoodMerchant f = new FoodMerchant();
            f.setProperties(foodMerchantVo.toMap());

            foodMerchantVo.setMerchant_site(dealAddress(foodMerchantVo));
            int temp = merchantService.saveFoodMerchant(f, foodMerchantVo.getPic_site());
            if (temp==1) {
                return Message.getReturnMsg(0, "");
            }
        }
        return "";
    }

    /**
     * 根据选择的value值替换成中文
     * @param foodMerchant
     * @return
     */
    public String dealAddress(FoodMerchantVo foodMerchant){
        String[] address=foodMerchant.getMerchant_site().split("-");
        String site="";
            if (address[0].equals("0")){
                site +="湖南省-";
            }else if(address[0].equals("1")){
                site +="湖北省-";
            }else if(address[0].equals("2")){
                site +="广西省-";
            }else if(address[0].equals("3")){
                site +="浙江省-";
            }else if(address[0].equals("4")){
                site +="河北省-";
            }

            if (address[1].equals("0")){
                site +="益阳市-";
            }else if(address[1].equals("1")){
                site +="杭州市-";
            }else if(address[1].equals("2")){
                site +="重庆市-";
            }else if(address[1].equals("3")){
                site +="长沙市-";
            }else if(address[1].equals("4")){
                site +="株洲市-";
            }

            if (address[2].equals("0")){
                site +="天心区";
            }else if(address[2].equals("1")){
                site +="雨花区";
            }else if(address[2].equals("2")){
                site +="望城区";
            }
        return site;
    }
}
